﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormAddStuff : FormTemplet
    {
        public FormAddStuff()
        {
            this.InitializeComponent();
        }

        private void FormAddStuff_Load(object sender, EventArgs e)
        {
            this.cbType.SelectedIndex = 0;
            this.dtpDate.Value = DateTime.Today;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.cbType.Text.Trim().Length > 32 || this.cbType.Text.Trim().Length < 1)
            {
                MessageBox.Show("类型 格式不正确，字符不能为空 或 大于 32", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.cbType.Focus();
            }
            else if (!Regex.IsMatch(this.txtLength.Text.Trim(), "^[0-9]{1,8}$") || Convert.ToInt32(this.txtLength.Text.Trim()) <= 0)
            {
                MessageBox.Show("长度 格式不正确，此处应该为大于零的正整数", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtLength.Focus();
            }
            else if (!Regex.IsMatch(this.txtWidth.Text.Trim(), "^[0-9]{1,8}$") || Convert.ToInt32(this.txtWidth.Text.Trim()) <= 0)
            {
                MessageBox.Show("宽度 格式不正确，此处应该为大于零的正整数", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtWidth.Focus();
            }
            else if (!Regex.IsMatch(this.txtQuantity.Text.Trim(), "^[0-9]{1,8}$") || Convert.ToInt32(this.txtQuantity.Text.Trim()) <= 0)
            {
                MessageBox.Show("宽度 格式不正确，此处应该为大于零的正整数", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtQuantity.Focus();
            }
            else if (this.txtRemark.Text.Trim().Length > 32)
            {
                MessageBox.Show("备注 格式不正确，字符不能大于 64", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.cbType.Focus();
            }
            else
            {
                Stuffs stuffs = new Stuffs();
                bool flag = false;
                try
                {
                    flag = stuffs.AddStuff(this.cbType.Text.Trim(), Convert.ToInt32(this.txtLength.Text.Trim()), Convert.ToInt32(this.txtWidth.Text.Trim()), Convert.ToInt32(this.txtQuantity.Text.Trim()), this.dtpDate.Value, this.txtRemark.Text.Trim(), LoginInfo.OperatorID);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                stuffs.Dispose();
                if (flag)
                {
                    MessageBox.Show("录入成功", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    if (MessageBox.Show("还继续吗 ?", "?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        this.txtLength.Text = "";
                        this.txtWidth.Text = "";
                        this.txtQuantity.Text = "";
                        this.txtRemark.Text = "";
                    }
                    else
                    {
                        base.Close();
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
